<template>
	<view>
		<view class="content" v-if="uploadImg">
			<image class="img mar-0-5" :src="baseURL+uploadImg"></image>
			<u-icon class="close" name="backspace" @click="clickClose"></u-icon>
		</view>
		<u-upload class="upload" v-else @afterRead="onSuccess" :maxCount="1"></u-upload>
	</view>
</template>

<script>
	import {
		mapGetters,
	} from 'vuex'
	export default {
		props: {
			path: String
		},
		data() {
			return {
				uploadImg: "", //上传后的新图片
				action: '',
			}
		},
		computed: {
			...mapGetters(['baseURL']),
		},
		mounted() {
			this.action = this.baseURL + "/upload/" + this.path
		},
		methods: {
			onSuccess(data) {
				let a = uni.uploadFile({
					url: this.action,
					filePath: data.file.url,
					name: "file",
					formData: {
						user: "test",
					},
					success: (res) => {
						let obj = JSON.parse(res.data);
						this.uploadImg = obj.result;
						this.$emit('onSuccess', this.uploadImg)
					},
				});
			},
			clickClose() {
				this.uploadImg = ''
			},
		}
	}
</script>

<style scoped>
.content {
	position: relative;
}

.content .close {
	position: absolute;
	right: 0;
	top: 0;
}

.img {
	width: 80px;
	height: 80px;
	margin: 0;
}

.u-upload__button {
	margin: 0 !important;
}
</style>